package com.hhss.qishi.service.system.impl;

import com.hhss.qishi.entity.system.Menu;
import com.hhss.qishi.mapper.system.MenuMapper;
import com.hhss.qishi.service.impl.CommonServiceImpl;
import com.hhss.qishi.service.system.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;

/**
 * @Author :Administrator
 * @Description
 * @Date :2019-05-05 14:05.
 */
@Service
public class MenuServiceImpl extends CommonServiceImpl<Menu, Serializable> implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	/**
	 * 计数
	 */
	private int count = 0;

	@Override
	public List<Menu> getList(Menu menu){
		return menuMapper.getList(menu);
	}

	@Override
	public List<Menu> getMenuTreeAsync(Menu menu) {
		return menuMapper.getMenuTreeByPid(menu);
	}

	public int updateByUuid(Menu menu){
		Example example = new Example(Menu.class);
		example.createCriteria().andEqualTo("uuid", menu.getUuid());
		return menuMapper.updateByExampleSelective(menu, example);
	}

	@Override
	public int deleteByUuid(String uuid){
		Menu menu = new Menu();
		menu.setUuid(uuid);
		return menuMapper.delete(menu);
	}

	@Override
	public int hasChildIfyDelete(String uuid){
		count ++;
		Menu menu = new Menu();
		menu.setPid(uuid);
		List<Menu> menus = menuMapper.getMenuTreeByPid(menu);
		if (menus != null && menus.size() > 0){
			for (Menu m : menus){
				hasChildIfyDelete(m.getUuid());
				deleteByUuid(m.getUuid());
			}
		}
		return count;
	}
}
